package com.weixx.yctp.cp.controller;

import cc.eamon.open.permission.Limit;
import cc.eamon.open.permission.annotation.Permission;
import cc.eamon.open.permission.annotation.PermissionLimit;
import cc.eamon.open.status.Status;
import cc.eamon.open.status.StatusBaseController;
import cc.eamon.open.status.StatusCode;
import com.weixx.yctp.cp.entity.CpPostMapper;
import com.weixx.yctp.cp.entity.CpUpdateMapper;
import com.weixx.yctp.cp.service.CpService;
import com.weixx.yctp.cp.service.PcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Long;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author: eamon
 * Email: eamon@eamon.cc */
@Api(
    value = "测评试卷-操作管理",
    tags = "测评试卷-操作管理"
)
@RestController
@RequestMapping("/cp")
@Permission
public class CpController extends StatusBaseController {
  @Autowired
  private CpService cpService;
  @Autowired
  private PcService pcService;

  @ApiOperation(
      value = "获取测评试卷-操作简要列表",
      notes = "获取测评试卷-操作简要列表"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getCpSimpleMapList(@RequestParam(required = false, defaultValue = "0") Long page,
      @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.getCpSimpleMapList(page, rows),
        cpService.getCpCount()
        );
  }


  @ApiOperation(
          value = "查看未标记的测评-查看信息",
          notes = "查看未标记的测评-查看信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "nomark",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getCpwithNoMark(@RequestParam(required = false, defaultValue = "0") Long page,
                                @RequestParam(required = false, defaultValue = "10") Integer rows,
                              HttpServletRequest request) throws Exception {
    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            cpService.getCpwithNoMark(page,rows),
            "查询成功");
  }


  @ApiOperation(
      value = "获取测评试卷-操作详情列表",
      notes = "获取测评试卷-操作详情列表"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "detail",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getCpDetailMapList(@RequestParam(required = false, defaultValue = "0") Long page,
      @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.getCpDetailMapList(page, rows),
        cpService.getCpCount()
        );
  }

  @ApiOperation(
      value = "通过主键获取测评试卷-操作简要信息",
      notes = "通过主键获取测评试卷-操作简要信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "/byKey",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getCpSimpleMap(@RequestParam Integer entityKey) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.getCpSimpleMapByPrimaryKey(entityKey),
        "查询成功");
  }

  @ApiOperation(
      value = "通过主键获取测评试卷-操作详细信息",
      notes = "通过主键获取测评试卷-操作详细信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "{entityKey}/detail",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getCpDetailMap(@PathVariable Integer entityKey) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.getCpDetailMapByPrimaryKey(entityKey),
        "查询成功");
  }

  @ApiOperation(
      value = "发布测评试卷-操作信息",
      notes = "发布测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "",
      method = RequestMethod.POST
  )
  @ResponseBody
  public Status postCpMap(@RequestBody CpPostMapper entityPostMapper, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    String ip = request.getHeader("x-forwarded-for");
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getHeader("Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getRemoteAddr();
    }
    ip = ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.postCp(entityPostMapper, ip, ip,ip),
        "发布成功");
  }

  @ApiOperation(
      value = "发布一组测评试卷-操作信息",
      notes = "发布一组测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.POST
  )
  @ResponseBody
  @PermissionLimit
  public Status postCpMapList(@RequestBody ArrayList<CpPostMapper> entityPostMapperList,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    String ip = request.getHeader("x-forwarded-for");
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getHeader("Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
      ip = request.getRemoteAddr();
    }
    ip = ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.postCpList(entityPostMapperList, userName, userName,ip),
        "发布成功");
  }

  @ApiOperation(
      value = "更新测评试卷-操作信息",
      notes = "更新测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "changepc/",
      method = RequestMethod.PUT
  )
  @ResponseBody
  @PermissionLimit
  public Status updateCpMap(@RequestBody CpUpdateMapper entityUpdateMapper, HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.updateCp(entityUpdateMapper, userName),
        "更新成功");
  }

  @ApiOperation(
      value = "更新一组测评试卷-操作信息",
      notes = "更新一组测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.PUT
  )
  @ResponseBody
  @PermissionLimit
  public Status updateCpMapList(@RequestBody ArrayList<CpUpdateMapper> entityUpdateMapperList,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.updateCpList(entityUpdateMapperList, userName),
        "更新成功");
  }

  @ApiOperation(
      value = "删除测评试卷-操作信息",
      notes = "删除测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "{entityKey}",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status deleteCpByPrimaryKey(@PathVariable Integer entityKey, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.deleteCp(entityKey, userName),
        "删除成功");
  }

  @ApiOperation(
      value = "删除一组测评试卷-操作信息",
      notes = "删除一组测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status deleteCpByPrimaryKeyList(@RequestBody ArrayList<Integer> keys,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.deleteCpList(keys, userName),
        "删除成功");
  }

  @ApiOperation(
      value = "恢复测评试卷-操作信息",
      notes = "恢复测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "{entityKey}/recover",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status recoverCpByPrimaryKey(@PathVariable Integer entityKey, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.recoverCp(entityKey, userName),
        "恢复成功");
  }

  @ApiOperation(
      value = "恢复一组测评试卷-操作信息",
      notes = "恢复一组测评试卷-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "recover/list",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status recoverCpByPrimaryKeyList(@RequestBody ArrayList<Integer> keys,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        cpService.recoverCpList(keys, userName),
        "恢复成功");
  }



  @ApiOperation(
          value = "根据时间查询测评时间-查询信息",
          notes = "根据时间查询测评时间-查询信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "queryByTime",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status queryByTime(@RequestParam String start, @RequestParam String end,@RequestParam(required = false, defaultValue = "0") Long page,
                            @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate=sdf.parse(start);
    Date endDate=sdf.parse(end);

    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            cpService.queryByTime(startDate,endDate,page,rows),
            "查询成功");
  }


  @ApiOperation(
          value = "根据批次查询测评-查询信息",
          notes = "根据批次查询测评-查询信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "queryByPc",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status queryByPc(@RequestParam String Pc,@RequestParam(required = false, defaultValue = "0") Long page,
                            @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {

    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            cpService.queryByPc(Pc,page,rows),
            "查询成功");
  }

  @ApiOperation(
          value = "根据批次查询平均评分-查询信息",
          notes = "根据批次查询平均评分-查询信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "PcAverage",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status queryByPcAverage(@RequestParam String pcTime) throws Exception {
    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            cpService.queryByPcAverage(pcTime),
            "查询成功");
  }



  @ApiOperation(
          value = "根据当前正在进行的批次查询所有记录-查询信息",
          notes = "根据当前正在进行的批次查询所有记录-查询信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "PcAverage/ondoing/{pcTime}",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status queryByondoing() throws Exception {
   // pcTime
    List<Map<String, Object>> onDongPc = pcService.getOnDongPc();
    List<String> idList=new ArrayList<>();
    for (Map<String, Object> stringObjectMap : onDongPc) {
      System.out.println(stringObjectMap.get("pcTime"));
      idList.add((String) stringObjectMap.get("pcTime"));
    }
    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            //根据id 的 list列表查询所有
            cpService.getCpDetailMapByPrimaryKeyList(idList),
            "查询成功");
  }

}
